//
//  CarColorEntity.m
//  webRequestText
//
//  Created by 肖扬 on 15/9/7.
//  Copyright (c) 2015年 肖扬. All rights reserved.
//

#import "CarColorEntity.h"

@implementation CarColorEntity
-(id) initWithFMDB:(FMDatabase *)database{
    self = [super initWithFMDB:database];
    if(self){
        //给表名赋值
        self.tableName = @"CarColor";
        //字段列表
        self.fields = @[@"KEYID",@"ID", @"ColorID", @"CarID", @"isDel", @"CreateTime", @"LastUpdate", @"Version"];
        //主键的ID
        //self.primaryField = @"id";
        [self setDefault];
    }
    return self;
}

//设置默认数据
-(void) setDefault{
    //清除数据
    self.KEYID = NSNotFound;
    self.ID = nil;
    self.isDel = nil;
    self.ColorID = nil;
    self.CreateTime = nil;
    self.LastUpdate = nil;
    self.Version = nil;
    self.CarID = nil;
}

//获取所有字段存到数据库的值
-(NSArray *) parameters{
    return @[[XYSqlite valueToString:self.ColorID],
             [XYSqlite valueToString: self.CreateTime],
             [XYSqlite valueToString: self.LastUpdate],
             [XYSqlite valueToString: self.Version],
             [XYSqlite valueToString: self.CarID],
             [XYSqlite valueToString: self.isDel],
             [XYSqlite valueToString: self.ID],
             [NSNumber numberWithInt: self.KEYID]];
    //           [XYSqlite dateToValue: self.timestamp],
}

//插入数据
-(int)insert{
    NSString *sql = @"INSERT INTO CarClass(%@) VALUES (%@)";
    NSString *insertFields = @"ID, ColorID, CarID, isDel, CreateTime, LastUpdate, Version";
    NSString *insertValues = @"?,?,?,?,?,?,?";
    NSLog(@"ID__%d", self.KEYID);
    //没有指定ID
    if(self.ID != nil){
        insertFields = [insertFields stringByAppendingString: @", KEYID"];
        insertValues = [insertValues stringByAppendingString: @", ?"];
    }
    sql = [NSString stringWithFormat: sql, insertFields, insertValues];
    return [self insertWithSql:sql parameters: [self parameters]];
}

//更新数据
-(BOOL)update{
    NSString *sql = @"UPDATE CarColor SET ColorID = ?, CreateTime = ?, LastUpdate = ?, Version = ?, CarID = ?, isDel = ? WHERE ID = ?";
    [self.database open];
    BOOL result = [self.database executeUpdate:sql withArgumentsInArray: [self parameters]];
    [self.database close];
    return result;
}

//转换字典到当前实例
-(void)parseFromDictionary: (NSDictionary *) data{
    self.data       = data;
    self.KEYID      = [[data objectForKey:@"id"] intValue];
    self.ID         = [XYSqlite valueToString: [data objectForKey:@"ID"]];
    self.isDel      = [XYSqlite valueToString: [data objectForKey:@"isDel"]];
    self.ColorID    = [XYSqlite valueToString: [data objectForKey:@"ColorID"]];
    self.CreateTime = [XYSqlite valueToString: [data objectForKey:@"CreateTime"]];
    self.LastUpdate = [XYSqlite valueToString: [data objectForKey:@"LastUpdate"]];
    self.Version    = [XYSqlite valueToString: [data objectForKey:@"Version"]];
    self.CarID      = [XYSqlite valueToString: [data objectForKey:@"CarID"]];
}

//获取一条数据
-(BOOL) findOneWithCondition:(NSString *)cond parameters:(NSArray *)params orderBy:(NSString *)orderBy{
    BOOL result = [super findOneWithCondition:cond parameters:params orderBy:orderBy];
    //将数据填充到属性
    if(result){
        [self parseFromDictionary: self.data];
    }else{
        //没有找到数据，还原为初始值
        [self setDefault];
    }
    return result;
}

//==================获取字段名及表名==================
//获取所有的字段名称
+(NSArray *) fields{
    return @[@"KEYID",@"ID", @"ColorID", @"CarID", @"isDel", @"CreateTime", @"LastUpdate", @"Version"];
}

//获取表名
+(NSString*)tableName{
    return @"CarColor";
}
//字段名：KEYID
+(NSString*)KEYID{
    return @"ID";
}

//字段名：ID
+(NSString*)ID{
    return @"ID";
}

//字段名： ColorID
+(NSString*)ColorID{
    return @"ColorID";
}

//字段名：CarID
+(NSString *) CarID{
    return @"CarID";
}

//字段名：isDel
+(NSString*)isDel{
    return @"isDel";
}

//字段名：CreateTime
+(NSString*)CreateTime{
    return @"CreateTime";
}

//字段名：LastUpdate
+(NSString *) LastUpdate{
    return @"LastUpdate";
}

//字段名：Version
+(NSString *) Version{
    return @"Version";
}

//创建数据库的sql语句
+(NSString *) sqlForCreateTable{
    return @"CREATE TABLE if not exists CarColor(KEYID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,ID TEXT, ColorID TEXT, CarID TEXT, isDel TEXT, CreateTime TEXT, LastUpdate TEXT, Version TEXT)";
}
@end

